//
//  FilterSortByViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/1.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "FilterSortByViewController.h"

#import "ACMacros.h"
#import "User.h"

#import <MobClick.h>

@interface FilterSortByViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation FilterSortByViewController {
    NSInteger selectedRow;
    NSMutableArray *labelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self insertBackground];
    [self configureTableView];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    if (!labelArray) {
        labelArray = [[NSMutableArray alloc]init];
    }
    [MobClick beginLogPageView:@"房间类型筛选页"];//("PageOne"为页面名称，可自定义)
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [MobClick endLogPageView:@"房间类型筛选页"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)insertBackground {
    UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [backgroundImageView setImage:[UIImage imageNamed:@"screen_bg"]];
    [self.view insertSubview:backgroundImageView atIndex:0];
}

- (void)configureTableView {
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    if (self.sourceDataArray == nil) {
        self.sourceDataArray = [[NSArray alloc]init];
    }
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    selectedRow = self.selectRow;
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 50)];
    [label setFont:[UIFont systemFontOfSize:15]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor whiteColor]];
    [label setText:self.sourceDataArray[row]];
    [labelArray addObject:label];
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    if (row == selectedRow) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [label setTextColor:RGBCOLOR(255, 118, 108)];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        [label setTextColor:RGBCOLOR(255, 255, 255)];
    }
    [cell addSubview:label];
    [cell setTintColor:[UIColor groupTableViewBackgroundColor]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setBackgroundColor:[UIColor clearColor]];
    
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryNone];
    UILabel *label = labelArray[selectedRow];
    [label setTextColor:RGBCOLOR(255, 255, 255)];
    selectedRow = row;
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryCheckmark];
    UILabel *newlabel = labelArray[selectedRow];
    newlabel = labelArray[selectedRow];
    [newlabel setTextColor:RGBCOLOR(255, 118, 108)];
    [self.delegate didSelectedRow:selectedRow WithItem:self.sourceDataArray[selectedRow]];
    [self dismissViewControllerAnimated:true completion:^{
    }];
}

#pragma mark - Triggering Methods
- (IBAction)clickToCancelBack:(UIButton *)sender {
    [self.delegate didSelectedRow:selectedRow WithItem:self.sourceDataArray[selectedRow]];
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}
@end
